package hust.ioic.oa.utils;

import hust.ioic.oa.domain.Function;
import hust.ioic.oa.domain.Operator;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class Installer {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	@Transactional
	public void install() {
		Session session = sessionFactory.getCurrentSession();
		// ====================设置超级管理员=====================
		Operator user = new Operator();
		user.setUsername("admin");
		user.setPassword(DigestUtils.md5Hex("1234"));
		user.setGender("男");
		user.setStatus(1);
		user.setIsAdmin(1);
		session.save(user);
		// ====================保存权限数据=====================
		Function menu, menu1, menu2, menu3, menu4;
		// --------------------
		menu = new Function("系统管理", null, null);
		menu1 = new Function("岗位管理", "/role_list", menu);
		menu2 = new Function("部门管理", "/department_list", menu);
		menu3 = new Function("操作员管理", "/operator_list", menu);
		menu4 = new Function("知识类型管理", "/knowType_list", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		//
		session.save(new Function("岗位列表", "/role_list", menu1));
		session.save(new Function("岗位删除", "/role_delete", menu1));
		session.save(new Function("岗位添加", "/role_add", menu1));
		session.save(new Function("岗位修改", "/role_edit", menu1));
		// // ---------------------
		
				session.save(new Function("部门列表", "/department_list", menu2));
				session.save(new Function("部门删除", "/department_delete", menu2));
				session.save(new Function("部门添加", "/department_add", menu2));
				session.save(new Function("部门修改", "/department_edit", menu2));
				// // --------------------
		session.save(new Function("操作员列表", "/operator_list", menu3));
		session.save(new Function("操作员删除", "/operator_delete", menu3));
		session.save(new Function("操作员添加", "/operator_add", menu3));
		session.save(new Function("操作员修改", "/operator_edit", menu3));
		session.save(new Function("初始化密码", "/operator_initPassword", menu3));
		
		//
		session.save(new Function("知识类型列表", "/knowType_list", menu4));
		session.save(new Function("知识类型删除", "/knowType_delete", menu4));
		session.save(new Function("知识类型添加", "/knowType_add", menu4));
		session.save(new Function("知识类型修改", "/knowType_edit", menu4));
		
		//
		menu = new Function("文件管理", null, null);
		//
		menu1 = new Function("文件分类", "/rdfile_knowList", menu);
		menu2 = new Function("我的文件", "/rdfile_myfileList", menu);
		menu3 = new Function("上传文件", "/rdfile_upload", menu);
		menu4 = new Function("审阅", "/messageQuene_checkList", menu);
		
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		// //
		
	
	}

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Installer installer = (Installer) ac.getBean("installer");
		System.out.println(installer);
		installer.install();
	}
}
